--#include "data\functions\Systems\CircleSystem\Circle_main.lua" once
--#include "data\config\property\LevelUpExp.lua" once
--此功能在用，2017.1


--转身处理
MiscsImplement.CircleHandle = function( sysarg, msgid, packet )
	local var = Actor.getStaticVar(sysarg)

	local cirlevel = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
	
	CircleHandleImpl(sysarg, cirlevel + 1)
end

function CheckCircleSoulExchange(sysarg,config)
	local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if level < config.reqLevel then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.c000007, config.reqLevel), ttFlyTip)
		return false
	end
	
	local cirlevel = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)

	local var = Actor.getStaticVar(sysarg)
	if not var.days then var.days = {} end	
	local useCircle = var.days.useCircle
	if useCircle ~= nil and useCircle >=  Circle.dailyCountLimit[cirlevel + 1] then
		-- 当天的转数已经用完
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.c000190, ttFlyTip)
		return false
	end

	if not CheckConsumeCond( sysarg, config.consumes, 1 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.coin00001, ttFlyTip)
		return false
	end

	
	return true
end

function SendActorLeftCircle(sysarg, useCircle)
	if useCircle == nil then
		local var = Actor.getStaticVar(sysarg)
		if not var.days then var.days = {} end
		useCircle = var.days.useCircle
		if useCircle == nil then
			useCircle = 0 -- 默认是3次
		end
	end
	
	-- 下发今天剩余转生次数(已经改为今天兑换修为剩余次数)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sSendLeftCircle)
	DataPack.writeByte(netPack, useCircle)	
	DataPack.flush(netPack)
end

--[[
function GetActorCircleCanReachLevel(sysarg,msgid,packet)
	local currExp = Actor.getUIntProperty(sysarg, PROP_ACTOR_EXP)		-- 玩家当前经验
	local toLevel = 0
	for idx = 1, 255 do
		if currExp < LevelUpExp[idx] then
			toLevel = idx - 1
			break
		else
			currExp = currExp - LevelUpExp[idx]
		end
	end

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sCircleReachLevel)
	DataPack.writeByte(netPack, toLevel)	
	DataPack.flush(netPack)
end
]]

function ClientGetActorLeftCircle(sysarg,msgid,packet)
	SendActorLeftCircle(sysarg, nil)
end

function HandleExchangeCircleSoul(sysarg)
	local actorLevel = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	local config = Circle.CircleSoulExchange[actorLevel - Circle.activeLevel]
	if not config then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.c000194, ttFlyTip)
		System.trace("Can not find CircleSoulExchange config,actorLevel="..actorLevel)
		return 1
	end

	local bCheck = CheckCircleSoulExchange(sysarg,config)
	if bCheck ~= true then return end 
	

	if not DoConsumeCond( sysarg, config.consumes, 1, 968, Lang.LogTips.l00186 ) then
		return
	end

	local var = Actor.getStaticVar(sysarg)
	if not var.days then var.days = {} end
	local useCircle = var.days.useCircle
	if useCircle == nil then -- 今天剩余的转数(默认是3次)
		useCircle = 1		
	else
		useCircle = useCircle + 1
	end
	var.days.useCircle = useCircle
		
	local currentSoul = Actor.getIntProperty(sysarg,PROP_ACTOR_CIRCLE_SOUL)
	Actor.setIntProperty(sysarg,PROP_ACTOR_CIRCLE_SOUL,currentSoul + config.addSoul)
	Actor.setLevel(sysarg,config.chgLevel)
	Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.c000196, config.addSoul, currentSoul + config.addSoul), ttFlyTip)


	--世界文明等级
	updateWorldCultureBuff(sysarg)
	
	-- 下发今天剩余转生次数(已经改为今天兑换修为剩余次数)
	SendActorLeftCircle(sysarg, useCircle)
	
	return
end

MiscsImplement.ExchangeCircleSoulHandle = function(sysarg,msgid,packet)
	HandleExchangeCircleSoul(sysarg)
end



MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cCircle, MiscsImplement.CircleHandle)
MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cExchangeCircleTimes, MiscsImplement.ExchangeCircleSoulHandle)
MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cGetLeftCircle, ClientGetActorLeftCircle)
